<script>
import KtMap from "/src/components/common/KtMap.vue";
import MapSelectDialog from "/src/views/manage/iot/project/components/MapSelectDialog.vue";
import iot from "/src/js/iot/index.js";

export default {
  components: {MapSelectDialog, KtMap},
  data() {
    return {
      iot,
      show: false,
      requestParam: {
        id: null,
        name: null,
        priority: null,
        dataTypeCode: null,
        address: null,
        register: null,
        dataPrecision: null,
        sensorTypeCode: null,
        unitCode: null,
        storageStrategy: null,
        formula: null,
        reverseFormula: null,
        deviceId: null,
        // /**
        //  * 属性默认值
        //  */
        //     @Column(name = "t_attr_default_value")
        // private String attrDefaultValue;
        //
        // /**
        //  * 属性单位
        //  */
        //     @Column(name = "t_attr_unit")
        // private String attrUnit;
        //
        // /**
        //  * 属性备注
        //  */
        //     @Column(name = "t_attr_remark")
        // private String attrRemark;
        attrDefaultValue: null,
        attrUnit: null,
        attrRemark: null,
        param:{
          // 属性种类编码
          typeCode: null,
          // 功能码
          functionCode: null,
          // 读写方式编码
          rwTypeCode: null,
          // 数据格式编码
          dataFormatCode: null,
          dataOrderCode: null
        }
      },
      loading: false
    }
  },
  mounted() {
  },
  methods: {
    genSn() {
      let ran = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L",
        "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z",
        "0", "1", "2", "3", "4", "5", "6", "7", "8", "9",
        "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l",
        "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"];
      let str = "";
      for (let i = 0; i < 16; i++) {
        let index = Math.floor(Math.random() * ran.length);
        str += ran[index];
      }
      this.requestParam.sn = str;
    },
    openNew(deviceId) {
      this.requestParam = {
        id: null,
        name: null,
        priority: null,
        dataTypeCode: iot.getDataTypeList()[0].value,
        address: null,
        register: null,
        dataPrecision: null,
        sensorTypeCode: null,
        unitCode: null,
        storageStrategy: null,
        formula: null,
        reverseFormula: null,
        attrDefaultValue: null,
        attrUnit: null,
        attrRemark: null,
        deviceId,
        param:{
          // 属性种类编码
          typeCode: null,
          // 功能码
          functionCode: null,
          // 读写方式编码
          rwTypeCode: null,
          // 数据格式编码
          dataFormatCode: null,
          dataOrderCode: null
        }
      };
      this.open(this.requestParam);
    },
    open(requestParam) {
      if (!requestParam) {
        this.requestParam = {
          id: null,
          name: null,
          priority: null,
          dataTypeCode: iot.getDataTypeList()[0].value,
          address: null,
          register: null,
          dataPrecision: null,
          sensorTypeCode: null,
          unitCode: null,
          storageStrategy: null,
          formula: null,
          reverseFormula: null,
          attrDefaultValue: null,
          attrUnit: null,
          attrRemark: null,
          deviceId: null,
          param:{
            // 属性种类编码
            typeCode: "default",
            // 功能码
            functionCode: null,
            // 读写方式编码
            rwTypeCode: null,
            // 数据格式编码
            dataFormatCode: null,
            dataOrderCode: null
          }
        };
        this.show = true;
        return;
      }
      this.requestParam = JSON.parse(JSON.stringify(requestParam));
      this.show = true;

    },
    // 关闭
    close() {
      this.show = false;
    },
    // 地图选择
    mapSelect(data) {
      this.requestParam.longitude = data.longitude + "";
      this.requestParam.latitude = data.latitude + "";
      // 保留后5位
      this.requestParam.longitude = this.requestParam.longitude.substring(0, this.requestParam.longitude.indexOf(".") + 6);
      this.requestParam.latitude = this.requestParam.latitude.substring(0, this.requestParam.latitude.indexOf(".") + 6);
    },
    toSave() {
      if (this.requestParam.uploadCycle) {
        this.requestParam.uploadCycle = parseInt(this.requestParam.uploadCycle);
      }
      if (this.requestParam.priority) {
        this.requestParam.priority = parseInt(this.requestParam.priority);
      }
      if (this.requestParam.projectId) {
        this.requestParam.projectId = parseInt(this.requestParam.projectId);
      }
      if (this.requestParam.uploadCycle === '0') {
        this.requestParam.uploadCycle = parseInt(this.requestParam.uploadCycle);
      }
      if (this.requestParam.priority === '0') {
        this.requestParam.priority = parseInt(this.requestParam.priority);
      }


      if (this.loading) {
        return;
      }




      this.loading = true;
      // element的loading
      let loading = this.$loading({
        lock: true,
        text: this.$i18n.zhToGlobal('保存中'),
        spinner: 'el-icon-loading',
        background: 'rgba(0, 0, 0, 0.7)'
      });

      this.$kt.request.send({
        uri: "/business-iot-web/admin/iotSensor/save",
        data: this.requestParam,
        stateSuccess: (res) => {
          this.$message.success(this.$i18n.zhToGlobal('保存成功'));
          this.close();
          this.loading = false;
          loading.close();
          this.$emit("saveSuccess");
          this.requestParam = {
            id: null,
            name: null,
            priority: null,
            dataTypeCode: iot.getDataTypeList()[0],
            address: null,
            register: null,
            dataPrecision: null,
            sensorTypeCode: null,
            unitCode: null,
            storageStrategy: null,
            formula: null,
            reverseFormula: null,
            attrDefaultValue: null,
            attrUnit: null,
            attrRemark: null,
            deviceId: null,
            param:{
              // 属性种类编码
              typeCode: null,
              // 功能码
              functionCode: null,
              // 读写方式编码
              rwTypeCode: null,
              // 数据格式编码
              dataFormatCode: null,
              dataOrderCode: null
            }
          };
        },
        stateFail: (err) => {
          this.$message.error(err.errMsg);
          this.loading = false;
          loading.close();
        }
      });
    }
  }
}
</script>

<template>
  <el-dialog
      draggable
      width="500px"
      v-model="show"
      :title="$i18n.zhToGlobal('保存传感器')"
      @close="close"
  >
    <el-scrollbar
        :style="{height: '400px',width:'100%'}">
      <el-form
          style="width: calc(100% - 20px)"
          label-position="top"
          :model="requestParam" label-width="80px">

        <el-row gutter="20">
          <el-col :span="24">
            <el-form-item :label="$i18n.zhToGlobal('传感器名称')" required>
              <el-input v-model="requestParam.name"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item :label="`${$i18n.zhToGlobal('优先级')}`">
              <el-input v-model="requestParam.priority"
                        type="number"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <!-- 地址号 -->
            <el-form-item :label="`${$i18n.zhToGlobal('地址号')}`" required>
              <el-input v-model="requestParam.address"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <!-- 寄存器号 -->
            <el-form-item :label="`${$i18n.zhToGlobal('寄存器号')}`" required>
              <el-input v-model="requestParam.register"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <!-- 数据精度 -->
            <el-form-item :label="`${$i18n.zhToGlobal('数据精度')}`">
              <el-select v-model="requestParam.dataPrecision">
                <el-option
                    v-for="item in [0,1,2,3,4,5,6]"
                    :key="item"
                    :label="item"
                    :value="item">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>

        <el-divider></el-divider>

        <el-form-item
          :label="`${$i18n.zhToGlobal('数据类型')}`">
          <!-- 单选 -->
          <el-radio-group v-model="requestParam.dataTypeCode">
            <el-radio
                v-for="item in iot.getDataTypeList()"
                :key="item.value"
                :value="item.value">
              {{ $i18n.zhToGlobal(item.label) }}
            </el-radio>
          </el-radio-group>
        </el-form-item>

        <el-row gutter="20"
        v-if="requestParam.dataTypeCode === 'dataType'">
          <el-col :span="24">
            <el-form-item
                :label="`${$i18n.zhToGlobal('传感器类型')}`" required>
              <el-select v-model="requestParam.sensorTypeCode">
                <el-option
                    v-for="item in iot.getSensorTypeList()"
                    :key="item.value"
                    :label="$i18n.zhToGlobal(item.label)"
                    :value="item.value">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item
                :label="`${$i18n.zhToGlobal('单位')}`" required>
              <el-select v-model="requestParam.unitCode">
                <el-option
                    v-for="item in iot.getUnitList()"
                    :key="item.value"
                    :label="$i18n.zhToGlobal(item.label)"
                    :value="item.value">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>


        <div v-if="requestParam.dataTypeCode === 'configType'">
          <el-form-item
              :label="`${$i18n.zhToGlobal('属性默认值')}`">
            <el-input v-model="requestParam.attrDefaultValue"></el-input>
          </el-form-item>
          <el-form-item
              :label="`${$i18n.zhToGlobal('属性单位')}`">
            <el-input v-model="requestParam.attrUnit"></el-input>
          </el-form-item>
          <el-form-item
              :label="`${$i18n.zhToGlobal('属性备注')}`" required>
            <el-input v-model="requestParam.attrRemark"></el-input>
          </el-form-item>
        </div>

        <el-divider></el-divider>

        <el-form-item
            :label="`${$i18n.zhToGlobal('存储策略')}`" required>
          <el-select v-model="requestParam.storageStrategy">
            <el-option
                :label="iot.getStorageStrategy(-2)"
                :value="-2">
            </el-option>
            <el-option
                :label="iot.getStorageStrategy(-1)"
                :value="-1">
            </el-option>
            <!-- 30秒 -->
            <el-option
                :label="iot.getStorageStrategy(30)"
                :value="30">
            </el-option>
            <!-- 1分钟 -->
            <el-option
                :label="iot.getStorageStrategy(60)"
                :value="60">
            </el-option>
            <!-- 5分钟 -->
            <el-option
                :label="iot.getStorageStrategy(300)"
                :value="300">
            </el-option>
            <!-- 10分钟 -->
            <el-option
                :label="iot.getStorageStrategy(600)"
                :value="600">
            </el-option>
            <!-- 30分钟 -->
            <el-option
                :label="iot.getStorageStrategy(1800)"
                :value="1800">
            </el-option>
            <!-- 1小时 -->
            <el-option
                :label="iot.getStorageStrategy(3600)"
                :value="3600">
            </el-option>
            <!-- 1天 -->
            <el-option
                :label="iot.getStorageStrategy(86400)"
                :value="86400">
            </el-option>
          </el-select>
        </el-form-item>
        <!--            公式处理(设备->平台)-->
        <el-form-item
            :label="`${$i18n.zhToGlobal('公式处理(设备->平台)')}`">
          <el-input
              :placeholder="`${$i18n.zhToGlobal('公式计算，如：x/100')}`"
              v-model="requestParam.formula"></el-input>
        </el-form-item>

        <!--            公式处理(平台->设备)-->
        <el-form-item
            :label="`${$i18n.zhToGlobal('公式处理(平台->设备)')}`">
          <el-input
              :placeholder="`${$i18n.zhToGlobal('如：x*100')}`"
              v-model="requestParam.reverseFormula"></el-input>
        </el-form-item>

        <el-divider></el-divider>

        <div class="param-box">
          <div class="param-title">
            {{ $i18n.zhToGlobal('参数配置') }}
          </div>
          <div class="in-box">
            <el-form label-position="top">
              <el-row gutter="20">
                <el-col :span="12">
                  <el-form-item
                      :label="`${$i18n.zhToGlobal('属性种类')}`">
<!--                    <el-input v-model="requestParam.param.typeCode"></el-input>-->
                    <el-select v-model="requestParam.param.typeCode">
                      <el-option
                          v-for="(item,index) in iot.getSensorParamTypeCodeList()"
                          :label="item.label"
                          :value="item.value">
                      </el-option>
                    </el-select>
                  </el-form-item>
                </el-col>

                <el-col :span="12">
                  <el-form-item
                      :label="`${$i18n.zhToGlobal('读写方式')}`">
<!--                    <el-input v-model="requestParam.param.rwTypeCode"></el-input>-->
                    <el-select v-model="requestParam.param.rwTypeCode">
                      <el-option
                          v-for="(item,index) in iot.getRwTypeCodeList()"
                          :label="item.label"
                          :value="item.value">
                      </el-option>
                    </el-select>
                  </el-form-item>
                </el-col>


                <el-col
                    v-if="requestParam.param.typeCode==='modbus'"
                    :span="12">
                  <el-form-item
                      :label="`${$i18n.zhToGlobal('功能码')}`">
<!--                    <el-input v-model="requestParam.param.functionCode"></el-input>-->
                    <el-select v-model="requestParam.param.functionCode">
                      <el-option label="01" value="1"></el-option>
                      <el-option label="02" value="2"></el-option>
                      <el-option label="03" value="3"></el-option>
                      <el-option label="04" value="4"></el-option>
                      <el-option label="10" value="10"></el-option>
                    </el-select>
                  </el-form-item>
                </el-col>

                <el-col
                    v-if="requestParam.param.typeCode==='modbus'"
                    :span="12">
                  <el-form-item
                      :label="`${$i18n.zhToGlobal('数据格式')}`">
<!--                    <el-input v-model="requestParam.param.dataFormatCode"></el-input>-->
                    <el-select v-model="requestParam.param.dataFormatCode">
<!--                      getDataFormatCodeList-->
                      <el-option
                          v-for="(item,index) in iot.getDataFormatCodeList()"
                          :label="item.label"
                          :value="item.value">
                      </el-option>
                    </el-select>
                  </el-form-item>
                </el-col>
                <el-col
                    v-if="requestParam.param.typeCode==='modbus'"
                    :span="12">
                  <el-form-item
                      :label="`${$i18n.zhToGlobal('数据顺序')}`">
                    <!-- <el-input v-model="requestParam.param.dataOrderCode"></el-input> -->
                    <el-select v-model="requestParam.param.dataOrderCode">

                      <el-option
                          v-for="(item,index) in iot.getDataOrderCodeList()"
                          :label="item.label"
                          :value="item.value">
                      </el-option>

                    </el-select>
                  </el-form-item>
                </el-col>
              </el-row>
            </el-form>
          </div>

        </div>
      </el-form>

    </el-scrollbar>

    <div style="height: 40px"></div>
    <el-button
        @click="toSave"
        type="primary"
        style="width: 100%;position: absolute;bottom: 0;left: 0;border-radius: 0 0 3px 3px"
    >
      {{ $i18n.zhToGlobal('保存') }}
    </el-button>
  </el-dialog>

  <map-select-dialog
      @select="mapSelect"
      ref="mapSelectDialog"></map-select-dialog>

</template>

<style scoped lang="scss">
.map-box {
  // 超出不显示
  overflow: hidden;
}

.gen-btn {
  cursor: pointer;
}

.gen-btn:hover {
  transform: scale(1.1);
}

.gen-btn:active {
  transform: scale(1);
}

.param-box{
  border: 1px solid #ebeef5;
  border-radius: 5px;
  margin-top: 10px;
  .param-title{
    font-size: 16px;
    font-weight: bold;
    margin-bottom: 10px;
    background-color: #f0f0f0;
    border: 20px 20px 0 0;
    padding: 10px;
    border-radius: 5px 5px 0 0;
  }
  .in-box{
    padding: 20px;
    box-sizing: border-box;
  }
}


</style>